/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.client.param;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.dom4j.Element;

import com.edi.Kola;
import com.edi.client.plugin.PluginResult;
import com.edi.exceptions.EdiParamException;
import com.edi.pubsub.EdiService;

public class EdiSPOClientParam extends EdiClientParam {
	private static final Log log = LogFactory.getLog(EdiSPOClientParam.class);
	private String serviceName;
	private EdiService ediService;
	private List<String> args; //服务参数集合
	private PluginResult result; //执行结果

	public EdiSPOClientParam(Element ediPara) {
		super(ediPara);
		args = new ArrayList<String>();
		serviceName = ediPara.elementText("serviceName");
		ediService = Kola.getServiceManager().getService(serviceName);

		log.debug("参数数量为："
				+ ediPara.element("parameters").elements("param").size());
		parseArgs(ediPara.element("parameters").elements("param"));

		//		params = new HashMap<String,String>();		
		//		
		//		Iterator<Element> paramsIter = ediPara.element("parameters").elementIterator("param");
		//		while(paramsIter.hasNext()){
		//			Element param = paramsIter.next();
		//			params.put(param.attributeValue("name"), param.getText());
		//		}
	}

	//	public EdiSPOClientParam(){
	//		
	//	}

	/**
	 * 解析客户端传递来的参数
	 * 
	 * @param paramList
	 */
	private void parseArgs(List<Element> paramList) {
		for (Element paramElement : paramList) {
			String arg = paramElement.getText();
			log.debug("参数值为：" + arg);

			args.add(arg);
		}
	}

	public void excuteClientParam() throws EdiParamException {
		if (ediService == null) {
			throw new EdiParamException("未找到该服务：" + serviceName);
		}
		try {
			result = ediService.execute(this);
			//判定是否为一个文件结果

			resultE = result.getAnswerXML();

			//resultE = ediService.execute(this).getAnswerXML();
		} catch (Exception e) {
			e.printStackTrace();
			throw new EdiParamException("服务执行失败：" + serviceName, e);
		}
	}

	public Element answerClinentParam() {
		return resultE;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public List<String> getArgs() {
		return this.args;
	}

	public PluginResult getResult() {
		return this.result;
	}

}
